//
//  EllipseCycleScrollView.m
//  NongWanCloud
//
//  Created by YueWen on 2018/2/1.
//  Copyright © 2018年 YueWen. All rights reserved.
//

#import "RITLEllipseCycleScrollView.h"
#import "EllipsePageControl+ItemControl.h"
#import <RITLKit.h>


/// 用于模拟读取父类的数据
@protocol EllipseCycleScrollViewSuper <NSObject>

@property (nonatomic, weak) UIView *pageControl;
@property (nonatomic, weak) UICollectionView *mainView;
@property (nonatomic, strong) NSArray *imagePathsGroup;
@property (nonatomic, weak) UICollectionViewFlowLayout *flowLayout;

- (int)currentIndex;
- (int)pageControlIndexWithCurrentCellIndex:(NSInteger)index;

@end


@interface SDCycleScrollView (EllipseCycleScrollView) <EllipseCycleScrollViewSuper,UIScrollViewDelegate>



@end

@interface RITLEllipseCycleScrollView ()


@end

/// 重写部分方法
@implementation RITLEllipseCycleScrollView

- (instancetype)init
{
    if (self = [super init]) {
        
        self.backgroundColor = UIColor.whiteColor;
        self.autoScroll = false;
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = UIColor.whiteColor;
    }
    
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self updateMargin];
    
    //修正
    if ([self.configuration respondsToSelector:@selector(ritlEllipseCycleScrollView:sizeForPageControl:)]) {
        self.pageControl.ritl_size = [self.configuration ritlEllipseCycleScrollView:self sizeForPageControl:self.pageControl.ritl_size];
        
    }else {//自动修正
        
        EllipsePageControl *pageControl = (EllipsePageControl *)self.pageControl;
        
        //计算宽度
        CGFloat width = MAX(0, pageControl.numberOfPages - 1) * pageControl.controlSize; // 所有的大小
        width += pageControl.controlSize * pageControl.scale; // 拉长的大小
        width += pageControl.controlSpacing * (MAX(0, pageControl.numberOfPages - 1));
        
        self.pageControl.ritl_width = width;
    }
    
    if (![self.configuration respondsToSelector:@selector(ritlEllipseCycleScrollViewConfiguratePageControlIsMiddle:)] || [self.configuration ritlEllipseCycleScrollViewConfiguratePageControlIsMiddle:self]){//居中
        
        //居中
        self.pageControl.ritl_originX = (self.ritl_width - self.pageControl.ritl_width) / 2.0;
        
    }else {
        
        self.pageControl.ritl_originX = self.ritl_width - self.pageControl.ritl_width - self.pageControlMarginRight;
    }
}



- (void)setupPageControl
{
    if (self.pageControl) { [self.pageControl removeFromSuperview]; }// 重新加载时数据请求
    if (self.imagePathsGroup.count == 0 || self.onlyDisplayText) return;
    if ((self.imagePathsGroup.count == 1) && self.hidesForSinglePage) return;
    
    int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:[self currentIndex]];
    
    EllipsePageControl *pageControl = [EllipsePageControl new];
    pageControl.numberOfPages = self.imagePathsGroup.count;
    pageControl.currentPage = indexOnPageControl;
    pageControl.controlSize = (8);
    pageControl.controlSpacing = (10);
    pageControl.currentColor = self.currentPageDotColor;
    pageControl.otherColor = self.pageDotColor;
    pageControl.userInteractionEnabled = false;
    self.pageControl = pageControl;

    if ([self.configuration respondsToSelector:@selector(ritlEllipseCycleScrollViewConfiguratePageControl:)]) {
        [self.configuration ritlEllipseCycleScrollViewConfiguratePageControl:self];
    }

    [self addSubview:pageControl];
}

- (EllipsePageControl *)el_pageControl
{
    return (EllipsePageControl *)self.pageControl;
}


#pragma mark - *************** ScrollViewDelegate ***************

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.imagePathsGroup.count) return; // 解决清除timer时偶尔会出现的问题
    int itemIndex = [self currentIndex];
    int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:itemIndex];
    
    if ([self.pageControl isKindOfClass:EllipsePageControl.class]) {

        ((EllipsePageControl *)self.pageControl).currentPage = indexOnPageControl;//修改进度
    }
}

- (BOOL)debugIndex
{
    return true;
}


@end
